package Servlets;

import PackDatos.GestionBD;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class srv_comboAlumnoCurso extends HttpServlet {

 @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
               
        ArrayList<Map> lstAlumnos = new ArrayList<Map>();
        ArrayList<Map> lstCursos = new ArrayList<Map>();
        HttpSession session = request.getSession(true);
        Connection cn = null;
        try {
            // conectamos la BD 
            GestionBD og = new GestionBD();
            cn = og.conectar();
            // obtenemos los profesores
            String query = "select id_alumno,alu_codigo,alu_nombre,alu_apellido from alumno where alu_estado='1'";
            Statement stm = cn.createStatement();
            ResultSet rs = stm.executeQuery(query);
            
            while (rs.next()) {
                Map rec = new HashMap();
                rec.put("id_alumno", rs.getInt("id_alumno"));
                rec.put("alu_codigo", rs.getString("alu_codigo"));
                rec.put("alu_nombre", rs.getString("alu_nombre"));
                rec.put("alu_apellido", rs.getString("alu_apellido"));
                lstAlumnos.add(rec);
            }
            session.setAttribute("alumnos",lstAlumnos);
            
            query = "select id_curso,cur_codigo,cur_nombre from curso where cur_estado='1'";
            stm = cn.createStatement();
            rs = stm.executeQuery(query);
            
            while (rs.next()) {
                Map rec = new HashMap();
                rec.put("id_curso", rs.getInt("id_curso"));
                rec.put("cur_codigo", rs.getString("cur_codigo"));
                rec.put("cur_nombre", rs.getString("cur_nombre"));
                lstCursos.add(rec);
            }
            session.setAttribute("cursos",lstCursos);
            request.setAttribute("mensaje", " ");
            stm.close();
            cn.close();
        } catch (Exception e) {
            System.out.println("Error: ");
        }
        RequestDispatcher rd = request.getRequestDispatcher("matricularAlumno.jsp");
        rd.forward(request, response);
    }
}
